﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Citizenship : IEquatable<Citizenship>
    {
        public void ImportList(List<Classes.Citizenship> citList)
        {
            var mdb = new MedregDBDataContext();
            var ListCit = new List<Citizenship>();
            foreach (var cit in citList)
            {
                Citizenship cit1 = new Citizenship() { ID = cit.ID, Name = cit.Name };
                Citizenship query;

                if (mdb.Citizenship.LongCount() == 0)
                {
                    mdb.Citizenship.InsertOnSubmit(cit1);
                }
                else
                {
                    try
                    {
                        query = mdb.Citizenship.Single(l => l.ID == cit1.ID);
                        query.Name = cit1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListCit.Contains(cit1))
                        {
                            mdb.Citizenship.InsertOnSubmit(cit1);
                            ListCit.Add(cit1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Citizenship other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
